package com.iflytek.exam.controller;

import com.iflytek.common.core.controller.BaseController;
import com.iflytek.common.core.domain.AjaxResult;
import com.iflytek.exam.domain.ExamSet;
import com.iflytek.exam.service.AnswerSubmitService;
import com.iflytek.exam.service.IExamSetService;
import com.iflytek.exam.vo.AnswersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/exam/paper")
public class AnswerSubmitController extends BaseController {

    @Autowired
    private IExamSetService examSetService;
    @Autowired
    AnswerSubmitService answerSubmitService;

    //@PreAuthorize("@ss.hasPermi('exam:paper:submit')")
    @PostMapping("/submit/{examId}")
    public AjaxResult handleAnswers(@RequestBody AnswersVo answersVo, @PathVariable("examId") Long examId) {
        //保存答案
        answerSubmitService.insertPaper(answersVo, examId);
        ExamSet examSet = examSetService.selectExamSetByExamId(examId);
        examSet.setRetakesNum(examSet.getRetakesNum() - 1);
        //允许考试的次数减一
        examSetService.updateExamSet(examSet);
        if (examSet.getRetakesNum() != 0) {
            return AjaxResult.success("提交成功,改试卷还有" + examSet.getRetakesNum() + "次重考机会!");
        }else {
            return AjaxResult.success("提交成功,改试卷不允许重考!");
        }
    }
}
